#!/usr/bin/env python
# coding=utf-8
#
# 添加新的Cron Job

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import util
import crondb
import common
import sys

class DeleteCronJobHandler(webapp.RequestHandler):
	def process(self):
		user = users.get_current_user()
		if user:
			if users.is_current_user_admin():
				# get post data
				id = self.request.get("id")
				if id:
					id = int(id)
				else:
					id = 0
				if id > 0:
					cron = crondb.getCronInfo(id)
					if cron:
						crondb.deleteCronInfo(cron)
						self.redirect('cronlist')
					else:
						self.response.out.write('Can not found cron job in database. ID=%s;' % id)
				else:
					self.response.out.write('Can not found cron job in database. ID=%s;' % id)
			else:
				common.writeNoPermissionMessage(self)
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.process()

	def post(self):
		self.process()

def main():
	application = webapp.WSGIApplication([('/deletecron', DeleteCronJobHandler)],
		debug=True)
	util.run_wsgi_app(application)


if __name__ == '__main__':
	main()
